package cn.xuchunh.campusbbs.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import cn.xuchunh.campusbbs.R;

/**
 * Created by XuChunH on 2015/7/24.
 */
public class LoadingLayout extends FrameLayout implements OnClickListener {

    public static final int STATE_LOADDING = 0;
    public static final int STATE_EMPTY = 1;
    public static final int STATE_FAIL = 2;
    public static final int STATE_SHOW_COTNENT = 3;

    private ImageView image;
    private ProgressBar progress;
    private TextView text;
    private View contentView;

    private int state = STATE_LOADDING;
    private int loadingTextRes = R.string.request_data;
    private int failTextRes = R.string.request_data_err;
    private int emptyTextRes = R.string.not_more_data;
    private int failImageRes = R.drawable.ic_info_white_36dp;
    private int emptyImageRes = R.drawable.ic_info_white_36dp;

    private OnLoadingStateChangeListener onLoadingStateChangeListener;

    public LoadingLayout(Context context) {
        super(context);
        init(context, null);
    }

    public LoadingLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        View contentView = LayoutInflater.from(context).inflate(R.layout.layout_loading, null);
        image = (ImageView) contentView.findViewById(R.id.layout_loading_image);
        progress = (ProgressBar) contentView.findViewById(R.id.layout_loading_progress);
        text = (TextView) contentView.findViewById(R.id.layout_loading_text);

        TypedArray ta = context.obtainStyledAttributes(attrs,
                R.styleable.LoadingLayout);
        String message = ta.getString(R.styleable.LoadingLayout_loadingFailMessage);
        if (TextUtils.isEmpty(message)) {
            text.setText(loadingTextRes);
        } else {
            text.setText(message);
        }

        Drawable drawable = ta.getDrawable(R.styleable.LoadingLayout_loadingFailImage);
        if (drawable != null) {
            image.setImageDrawable(drawable);
        }
        contentView.setOnClickListener(this);
        addView(contentView);
        ta.recycle();
    }

    public void setLoadingTextRes(int loadingTextRes) {
        this.loadingTextRes = loadingTextRes;
    }

    public void setFailTextRes(int failTextRes) {
        this.failTextRes = failTextRes;
    }

    public void setEmptyTextRes(int emptyTextRes) {
        this.emptyTextRes = emptyTextRes;
    }

    public void setFailImageRes(int failImageRes) {
        this.failImageRes = failImageRes;
    }

    public void setEmptyImageRes(int emptyImageRes) {
        this.emptyImageRes = emptyImageRes;
    }

    public void setOnLoadingStateChangeListener(OnLoadingStateChangeListener onLoadingStateChangeListener) {
        this.onLoadingStateChangeListener = onLoadingStateChangeListener;
    }

    public void setState(int state) {
        if (state > STATE_SHOW_COTNENT || this.state == state) {
            return;
        }
        this.state = state;
        if (state == STATE_SHOW_COTNENT) {
            if (contentView != null) {
                contentView.setVisibility(View.VISIBLE);
                image.setVisibility(View.GONE);
                progress.setVisibility(View.GONE);
                text.setVisibility(View.GONE);
            }
            return;
        }
        if (contentView != null) {
            contentView.setVisibility(View.GONE);
        }
        if (state == STATE_LOADDING) {
            image.setVisibility(View.GONE);
            progress.setVisibility(View.VISIBLE);
            text.setText(loadingTextRes);
            text.setVisibility(View.VISIBLE);
        } else if (state == STATE_EMPTY) {
            image.setImageResource(emptyImageRes);
            image.setVisibility(View.VISIBLE);
            progress.setVisibility(View.GONE);
            text.setText(emptyTextRes);
            text.setVisibility(View.VISIBLE);
        } else if (state == STATE_FAIL) {
            image.setImageResource(failImageRes);
            image.setVisibility(View.VISIBLE);
            progress.setVisibility(View.GONE);
            text.setText(failTextRes);
            text.setVisibility(View.VISIBLE);
        }
        setVisibility(View.VISIBLE);
    }

    public int getState() {
        return state;
    }

    @Override
    public void onClick(View v) {
        int old = getState();
        if (state == STATE_EMPTY || state == STATE_FAIL) {
            setState(STATE_LOADDING);
            if (onLoadingStateChangeListener != null) {
                onLoadingStateChangeListener.onLoadingStateChange(this, old, STATE_LOADDING);
            }
        }
    }

    public interface OnLoadingStateChangeListener {
        void onLoadingStateChange(LoadingLayout loadingLayout, int oldState, int currentState);
    }

    public View getContentView() {
        return contentView;
    }

    public void setContentView(View contentView) {
        this.contentView = contentView;
        addView(contentView);
        if (state != STATE_SHOW_COTNENT) {
            contentView.setVisibility(View.GONE);
        }
    }
}
